<?php
/*
+--------------------------------------------------------------------------
|	Move Backup Files to Rackspace Could Files
|   By Joshua Martinez
|   You are free to use this script any way you want.
|   http://www.joshuagmartinez.com
|   ========================================
|   Date: Friday, 10th September 2010
|	========================================
+--------------------------------------------------------------------------
*/
///////////////////////////////////////////////////////////////////////////////////
///THERE IS NO NEED TO EDIT THIS FILE
///////////////////////////////////////////////////////////////////////////////////

// INCLUDE THE VARIABLES
require('variables.inc.php');

// INCLUDE THE CLOUDFILES API
require('cloudfiles/cloudfiles.php');

// CONNECT TO CLOUDFILES
$auth = new CF_Authentication($USERNAME, $KEY);
$auth->authenticate();
$conn = new CF_Connection($auth);
 
// GET THE CONTAINER TO USE
$container = $conn->create_container($NAME);
$container = $conn->get_container($NAME);
print "Using Containter \"".$NAME."\"\n\n";

// DATABASE BACKUP INFO
$mysqllocalfile = $SSPATH."/".$BACKUPDIR."/mysql-".$date.".bz2";
$mysqlfilename = "mysql-".$date.".bz2";
 
// UPLOAD DATABASE BACKUP
$object = $container->create_object($mysqlfilename);
$object->load_from_filename($mysqllocalfile);
print "Moved the \"".$mysqlfilename."\" file into the \"".$NAME."\" container\n";

// WP-CONTENT BACKUP INFO
$wpcontentlocalfile = $SSPATH."/".$BACKUPDIR."/wp-content-".$date.".tgz";
$wpcontentfilename = "wp-content-".$date.".tgz";
 
// UPLOAD WP-CONTENT BACKUP
$object = $container->create_object($wpcontentfilename);
$object->load_from_filename($wpcontentlocalfile);
print "Moved the \"".$wpcontentfilename."\" file into the \"".$NAME."\" container\n\n";

// REMOVE OLD FILES
// EXPECT AN ERROR ON THIS PART OF THE SCRIPT UNTIL YOU HAVE ENOUGH BACKED UP FILES TO BEGIN REMOVING THE OLD ONES
print "You set the default period of time to delete old backups to \"".$KEEPFOR."\" days.  If you are seeing errors below this line it may be because the script cannot find anything to remove.  We are working to fix this false error.  Check back for an update soon.\n\n";
$removedate = date('Y-m-d', strtotime("+".$KEEPFOR." days"));
$oldwpcontentbackup = "wp-content-".$removedate.".tgz";
$oldmysqlbackup = "mysql-".$removedate.".bz2";

$object = $container->delete_object($oldmysqlbackup);
print "Removed the \"".$oldmysqlbackup."\" file because it is ".$KEEPFOR." days old\n";

$object = $container->delete_object($oldwpcontentbackup);
print "Removed the \"".$oldwpcontentbackup."\" file because it is ".$KEEPFOR." days old\n";
?>